var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var session = require("express-session");
var canvas = require('canvas');
const axios = require('axios');
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;

var usersRouter = require('./routes/users');
var captchaRouter = require('./routes/captcha');
var signInRouter = require('./routes/signIn');
var answer1Router = require('./routes/answer1');
var answer2Router = require('./routes/answer2');
var getAnswer2_1Router = require('./routes/getAnswer2_1');
var getAnswer2_2Router = require('./routes/getAnswer2_2');
var getAnswer3Router = require('./routes/getAnswer3');
var xietiaoRouter = require('./routes/xietiao');
var changePasswordRouter = require('./routes/changePassword');

//var emailRouter = require('./routes/email');

var showExpertRouter = require('./routes/showExpert');
var questionaireRouter = require('./routes/questionaire');
var checkRouter = require('./routes/check');
// var emailRouter = require('./routes/email');
// var dbRouter = require('./routes/db');

var app = express();
const multer = require("multer")
const storage = multer.memoryStorage() // or use disk storage
const upload = multer({
  storage
})

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(session({
  secret: 'keyboard cat',
  resave: true,
  saveUninitialized: true
}))
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({
  extended: false
}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));




app.use('/users', usersRouter);
app.use('/captcha', captchaRouter);
app.use('/signIn', signInRouter);
app.use('/answer1', answer1Router);
app.use('/answer2', answer2Router);
app.use('/getAnswer2_2', getAnswer2_2Router);
app.use('/getAnswer2_1', getAnswer2_1Router);
app.use('/getAnswer3', getAnswer3Router);
app.use('/xietiao', xietiaoRouter);
app.use('/showExpert', showExpertRouter);
app.use('/changePassword', changePasswordRouter);
app.use('/questionaire', questionaireRouter);
app.use('/check', checkRouter);
// app.use('/db', dbRouter);
// catch 404 and forward to error handler
app.use(function (req, res, next) {
  next(createError(404));
});

// error handler
app.use(function (err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  console.log(err.message);
  res.render('error');
});

module.exports = app;

//session
app = express();
var session = require('express-session');
app.use(session({
  secret: 'keyboard cat',
  resave: false,
  saveUninitialized: true,
  cookie: {
    secure: false
  }
}));